﻿using UnityEngine;
using System.Collections;


class FrameData : object
{
    public int dir = -1; // 玩家方向度数， -1为未输入 
    public int op = -1;//玩家操作，默认都是-1，为方向
    public int fps = 0;//帧数信息
    public int no = 0;//玩家编号

    public FrameData()
    {


    }

    public string toJson()
    {
        string s = "dir:" + dir.ToString() + ",";

        s += "op:" + op.ToString() + ",";
        s += "no:" + no.ToString() + ",";
        s += "fps:" + fps.ToString() + ",";

        return s;

    }

    public string toUploadJson()
    {
        //转换为 上传服务器的json数据，不包含fps等非关键信息，节省流量
        string s = "dir:" + dir.ToString() + ",";
        s += "op:" + op.ToString() + ",";
        s += "no:" + no.ToString() + ",";
        return s;

    }
    public static FrameData CreateWithJson(string json)
    {
        FrameData ret = new FrameData();
        if (ret != null && ret.setJson(json))
        {
            return ret;
        }
        return null;
    }

    public static ArrayList CreateWithMultiJson(string json)
    {

        ArrayList ret = new ArrayList();
        int last = 0;
        for (int i = 0; i < json.Length; i++)
        {
            if (json[i].Equals('+'))
            {
                string json1 = json.Substring(last, i - last);
                last = i + 1;

                FrameData d = new FrameData();
                if (d != null && d.setJson(json1))
                {
                    ret.Add(d);
                }
            }
        }

        return ret;
    }

  
    public bool setJson(string json)
    {
        int last = 0;


        string k = "0", v = "0";
        for (int i = 0; i < json.Length; i++)
        //  foreach( char ch in json)
        {
            char ch = json[i];
            if (ch.Equals(':'))
            {
                k = json.Substring(last, i - last);
                last = i + 1;
            }

            if (ch.Equals(','))
            {
                v = json.Substring(last, i - last);
                last = i + 1;


                if (k == "dir")
                {

                    dir = int.Parse(v);
                }

                if (k == "op")
                {
                    op = int.Parse(v);
                }

                if (k == "fps")
                {
                    fps = int.Parse(v);
                }

                if (k == "no")
                {
                    no = int.Parse(v);
                }

                //  Debug.Log(k + "             " + v);
            }

        }
        return true;

    }
}
